#include "Storage/ConnectionConfig.hpp"
#include <iostream>

namespace Storage
{

ConnectionConfig::ConnectionConfig( const std::string& typeName, const std::string& name)
	: name_( name)
	, typeName_( typeName)
{
}

// ---------------------------------------------------------------------------------------------------------------------

ConnectionConfig::~ConnectionConfig()
{
}

// ---------------------------------------------------------------------------------------------------------------------

void ConnectionConfig::store( std::ostream& os)
{
	os << typeName_ << '\t' << this->name() << std::endl ;
	AttributeCollection::const_iterator iter = attributes_.begin(),
		endIter = attributes_.end();
	while( iter != endIter)
	{
		os << '\t' << iter->first << "\t" << iter->second << std::endl;
		++iter;
	}
}

// ---------------------------------------------------------------------------------------------------------------------

void ConnectionConfig::load( std::istream& is)
{
	// TODO: Make loading safer (checking for syntax errors)
	while( !is.eof())
	{
		if( '\t' != is.peek())
		{
			break; // end of connection
		}
		std::string attrName;
		std::string attrValue;
		is >> attrName;
		// Skip tab
		char ch;
		is.get( ch);
		std::getline( is, attrValue);
		attributes_[ attrName] = attrValue;
	}
}

} // namespace Storage
